package cn.sdc.sfw.dao.handler.impl;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cn.sdc.sfw.dao.handler.ResultSetHandler;
import cn.sdc.sfw.dao.model.PropertyType;
import cn.sdc.sfw.dao.types.DbType;
import cn.sdc.sfw.dao.types.DbTypes;

public class ObjectResultSetHandlerImpl implements ResultSetHandler {

	private Boolean listFlg;
	
	public ObjectResultSetHandlerImpl() {
		this.listFlg = false;
	}

	public ObjectResultSetHandlerImpl(Boolean listFlg) {
		this.listFlg = listFlg;
	}

	
	public Object handle(ResultSet rs) throws SQLException {
		if(listFlg){
			List list = new ArrayList();
			while (rs.next()) {
				ResultSetMetaData rsmd = rs.getMetaData();
				DbType dbType = DbTypes
						.getDbType(rsmd.getColumnType(1));
				list.add(dbType.getValue(rs, 1));
			}
			return list;
		}else if (rs.next()) {
			ResultSetMetaData rsmd = rs.getMetaData();
			DbType dbType = DbTypes
					.getDbType(rsmd.getColumnType(1));
			return dbType.getValue(rs, 1);
		} else {
			return null;
		}
	}
}